package cn.edu.zut.cs.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
    拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session= request.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser!=null){
            session.setAttribute("sess","已登录，欢迎进入");
            return true;
        }else{

            session.setAttribute("sess","未登录，请先登录");
            request.getRequestDispatcher("/index").forward(request,response);
            return false;
        }

    }


}
